/*
 * AddonStub.cpp
 *
 *  Created on: 2010-1-11
 *      Author: PeterMei
 */

#include "addon_features.h"
#include "addon_std.h"
#include "addon_objbase.h"
#include "addon_entry.h"
#include "addon_command.h"
#include "addon_graphics.h"
#include "cx_string.h"
#include "cx_memory.h"
#include "AddonBookCityStub.h"
//#ifndef TESTDOWNLOADCOMPONENT	
#include "BookCityViewObserver.h"
//#else
//#include "cx_DownloadObserver.h"
//#endif
#include "BookCityView.h"
#include "QWString.h"
#include "cx_moduleinterface.h"
#include "QQReaderMessageId.h"
#include "QQReaderModuleId.h"
#include "QTextSetting.h"
#include "QInstance.h"
#include "QFile.h"
#include "reader_model_bookstore.h"
#define IMAGE_FILE_POSTFIX L".png"
#define FILE_POSTFIX L".txt"
#define BAK_FILE_POSTFIX L".bak"
#define MP3_FILE_POSTFIX L".mp3"

CAddonBookCityStub::CAddonBookCityStub()
	{
	iAddonView = NULL;
	iAddonObserver = NULL;
	iDownloadNotifier = NULL;
	iImageNotifier = NULL;
	iUpdateNotifier = NULL;
	update_seq = 0;
	}

CAddonBookCityStub::~CAddonBookCityStub()
	{
	//#ifndef TESTDOWNLOADCOMPONENT				
	OBJECT_SAFE_RELEASE( iAddonObserver );
	//#else
	//	COMMO_SAFE_RELEASE( iAddonObserver );
	//#endif	
	OBJECT_SAFE_RELEASE( iAddonView );
	iOnlineNotifier.RemoveAll();
	iNotifier.RemoveAll();
	}

QBOOL CAddonBookCityStub::Start()
	{
	unsigned long ret_val = ADDON_OK;

	if (NULL == iAddonView)
		{
		iAddonView = addon_create_book_city_view(NULL);
		if (NULL == iAddonView)
			return QFALSE;

		IAddonBookCityView_SetUpdateData(iAddonView,Upgrade_time,update_seq);
		}

	if (NULL == iAddonObserver)
		{
		//#ifndef TESTDOWNLOADCOMPONENT			
		iAddonObserver = addon_create_bookcity_observer(NULL);
		if (NULL == iAddonObserver)
			return QFALSE;

		IAddonBookCityObserver_SetUserData( iAddonObserver, (void *)this );
		IAddonBookCityObserver_SetCallback( iAddonObserver, CAddonBookCityStub::__OnObserverCallback );
		IAddonBookCityObserver_SetDownloadCallback( iAddonObserver, CAddonBookCityStub::__OnDownloadObserverCallback );
		IAddonBookCityObserver_SetUpdateCallback( iAddonObserver, CAddonBookCityStub::__OnUpdateObserverCallback );
		ret_val = IAddonBookCityView_SetObserver( iAddonView, iAddonObserver );
		//#else
		//		iAddonObserver = comm_cx_create_download_observer( NULL );
		//		
		//		if ( NULL == iAddonObserver )
		//			return QFALSE;
		//		IComm_cx_DownLoadObserver_SetUserData( iAddonObserver, (void *)this );
		//		IComm_cx_DownLoadObserver_SetCallback( iAddonObserver, (comm_obsvr_fptr_download_t)CAddonBookCityStub::__OnObserverCallback );
		//		IComm_cx_DownLoadObserver_SetDownloadCallback( iAddonObserver, (comm_obsvr_fptr_download_t)CAddonBookCityStub::__OnDownloadObserverCallback );
		//		IComm_cx_DownLoadObserver_SetUpdateCallback( iAddonObserver, (comm_obsvr_fptr_download_t)CAddonBookCityStub::__OnUpdateObserverCallback );
		//		IComm_cx_DownLoadObserver_SetStoreCallback( iAddonObserver, (comm_obsvr_fptr_download_t)CAddonBookCityStub::__OnStoreObserverCallback );
		//		ret_val = IAddonBookCityView_SetObserver( iAddonView, (ADDON_REF(IObject))iAddonObserver);		
		//#endif		
		}

	if (ADDON_OK != ret_val)
		goto __FAILED;

	//#ifndef TESTDOWNLOADCOMPONENT		
	//#else	
	//	IAddonBookCityView_GetDownloadInfoList(iAddonView,NULL);	
	//	CQTextSetting* setting = (CQTextSetting*)CQInstance::GetByUid(CQInstance::ETextSettingInstance);
	//	IAddonBookCityView_SetIap( iAddonView, setting->GetAPNID());		
	//#endif	

	return QTRUE;

	__FAILED:
		{
		IAddonEntry_SetObserver( iAddonView, NULL );
		}
	return QFALSE;
	}

QBOOL CAddonBookCityStub::Request(CQWString& url, CQWString& property,
		CQWString& value, QINT aPageID)
	{
	if (NULL == iAddonView)
		return ADDON_FAILED;

	unsigned long ret_val;

	property_data_t property_data;
	property_data.property = NULL;
	property_data.value = NULL;

	if (property.GetLength() && value.GetLength())
		{
		property_data.property = (unsigned short *) (property.GetDataPtr());
		property_data.value = (unsigned short *) (value.GetDataPtr());
		ret_val = IAddonBookCityView_Request(iAddonView,(unsigned short *)(url.GetDataPtr()),&property_data,aPageID);
		}
	else
		{
		ret_val = IAddonBookCityView_Request(iAddonView,(unsigned short *)(url.GetDataPtr()),NULL,aPageID);
		}
	return ret_val;
	}

QBOOL CAddonBookCityStub::Download(CQWString& url)
	{
	if (NULL == iAddonView)
		return ADDON_FAILED;

	unsigned long ret_val;

	char *dst_url = NULL;
	int len = 0;
	if (url.GetDataPtr())
		{
		len = cx_strlen_w((CONST QWCHAR*) url.GetDataPtr());
		dst_url = (char *) cx_malloc(UTF8_ENCODING_LENGTH*(len + 1));
		if (NULL == dst_url)
			return ADDON_FAILED;

		cx_memset(dst_url, 0, UTF8_ENCODING_LENGTH*(len + 1));
		xml_ucs2_to_utf8_string((unsigned char *) dst_url, UTF8_ENCODING_LENGTH
				* (len + 1), (unsigned char *) url.GetDataPtr());
		}

	ret_val = IAddonBookCityView_DownloadRequest(iAddonView,QNULL,(void*)dst_url);

	cx_free(dst_url);

	return ret_val;
	}

void CAddonBookCityStub::Search(CQWString& url, int style, CQWString& key,
		QINT aPageID)
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_Search(iAddonView,(unsigned short *)(url.GetDataPtr()),style,(unsigned short *)(key.GetDataPtr()),aPageID);
	}

void CAddonBookCityStub::CancelRequest()
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_CancleRequest(iAddonView);
	}

QVOID CAddonBookCityStub::CancelUpgrade()
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_CancelUpgrade(iAddonView);
	}

void CAddonBookCityStub::__OnObserverCallback(ADDON_REF(IObject)sender , int ntf_status, void * ntf_data )
	{
	unsigned long ret_val;
	CAddonBookCityStub * thiz_obj;
	//#ifndef TESTDOWNLOADCOMPONENT		
		ret_val = IAddonBookCityObserver_GetUserData( sender, (void **)&thiz_obj );
		//#else
		//	ret_val = IComm_cx_DownLoadObserver_GetUserData( (COMM_REF(ICommO))sender, (void **)&thiz_obj );
		//#endif	
		if ( ADDON_OK != ret_val )
		return;

		if ( thiz_obj->iNotifier.GetCount() )
			{
			for (int i=0;i<thiz_obj->iNotifier.GetCount();i++)
				{
				MQHttpNotifier* n = thiz_obj->iNotifier.GetAt(i);
				if(!n) continue;
				n->HandleHttpEvent(ntf_status, ntf_data);
				}
			}
		}

void CAddonBookCityStub::__OnDownloadObserverCallback(ADDON_REF(IObject)sender , int ntf_status, void * ntf_data )
	{
	unsigned long ret_val;
	CAddonBookCityStub * thiz_obj;

	ret_val = IAddonBookCityObserver_GetUserData( sender, (void **)&thiz_obj );

	if ( ADDON_OK != ret_val )
	return;

	switch(ntf_status)
		{
		case EQAddTaskType:
			{
			if(ntf_data)
				{
				addtask_type_data_t* dataPtr = (addtask_type_data_t*)ntf_data;
				CQWString filename;
				filename.AppendData((LPCWSTR)(dataPtr->filename),cx_strlen_w(dataPtr->filename));

				CQWString url;
				url.AppendData((LPCWSTR)(dataPtr->url),cx_strlen_w(dataPtr->url));

				CQWString ext = CQFile::Ext(url);
				if(dataPtr->eventtype == EQDownloadEventType)
					{
					if((ext.CompareNoCase((LPCWSTR)MP3_FILE_POSTFIX) ==0) && 
							thiz_obj->iDownloadNotifier)
						thiz_obj->iDownloadNotifier->NotifyAddDownTask(filename,url,dataPtr->status);
					else if(ext.CompareNoCase((LPCWSTR)IMAGE_FILE_POSTFIX) == 0 && thiz_obj->iImageNotifier)
						{
						thiz_obj->iImageNotifier->NotifyAddDownTask(filename,url,dataPtr->status);
						cx_free(dataPtr->filename);
						}
				}
			}
			else
			{
//				CQWString filename;
//				filename.Empty();
//
//				CQWString url;
//				url.Empty();
//				thiz_obj->iDownloadNotifier->NotifyAddDownTask(filename,url,-1);
			}
		}
		break;
	case EQStatusType:
		{
			if(ntf_data)
			{
				status_type_data_t* dataPtr = (status_type_data_t*)ntf_data;
				CQWString filename;
				filename.AppendData((LPCWSTR)(dataPtr->filename),cx_strlen_w(dataPtr->filename));
				if(dataPtr->filename)
					cx_free(dataPtr->filename);

				CQWString url;
				url.AppendData((LPCWSTR)(dataPtr->url),cx_strlen_w(dataPtr->url));
				if(dataPtr->url)
					cx_free(dataPtr->url);

				if(dataPtr->eventtype == EQDownloadEventType)
				{
					CQWString ext = CQFile::Ext(filename);

					if((ext.CompareNoCase((LPCWSTR)MP3_FILE_POSTFIX) == 0 ) && thiz_obj->iDownloadNotifier)
						thiz_obj->iDownloadNotifier->HandleDownloadStatus(filename,url,dataPtr->status,dataPtr->data);
					else if(ext.CompareNoCase((LPCWSTR)IMAGE_FILE_POSTFIX) == 0 && thiz_obj->iImageNotifier)
						thiz_obj->iImageNotifier->HandleDownloadStatus(filename,url,dataPtr->status,dataPtr->data);
				}
				else if(dataPtr->eventtype == EQOnlineEventType && thiz_obj->iOnlineNotifier.GetCount())
				{
					CQWString bookname;						
					if(dataPtr->bookname)
					{
						bookname.AppendData((LPCWSTR)(dataPtr->bookname),cx_strlen_w(dataPtr->bookname));
						cx_free(dataPtr->bookname);
					}
					for (int i=0;i<thiz_obj->iOnlineNotifier.GetCount();i++)
					{
						MQOnlineNotifier* n = thiz_obj->iOnlineNotifier.GetAt(i);
						if(!n) continue;
						n->HandleOnlineStatus(filename,bookname,dataPtr->status,dataPtr->data);
					}
				}
			}
		}
		break;
	case EQEventType:
		{
			if(ntf_data)
			{
				event_data_t* dataPtr = (event_data_t*)ntf_data;

				CQWString filename;
				filename.AppendData((LPCWSTR)(dataPtr->filename),cx_strlen_w(dataPtr->filename));

				if(dataPtr->eventtype == EQOnlineEventType && thiz_obj->iOnlineNotifier.GetCount())
				{
					for (int i=0;i<thiz_obj->iOnlineNotifier.GetCount();i++)
					{
						MQOnlineNotifier* n = thiz_obj->iOnlineNotifier.GetAt(i);
						if(!n) continue;
						n->HandleOnlineEvent(filename,dataPtr->event,dataPtr->data);
					}
				}
			}
		}
		break;
	case EQProgressType:
		{
			if(ntf_data)
			{
				progress_data_t* dataPtr = (progress_data_t*)ntf_data;
				CQWString filename;
				filename.AppendData((LPCWSTR)(dataPtr->filename),cx_strlen_w(dataPtr->filename));

				if(dataPtr->eventtype == EQDownloadEventType && thiz_obj->iDownloadNotifier)
				{
					if(dataPtr->totalSize == 0)
						thiz_obj->iDownloadNotifier->NotifyProgress(filename,dataPtr->received);
					else
						thiz_obj->iDownloadNotifier->NotifyProgress(filename,dataPtr->received,dataPtr->totalSize,dataPtr->per);
				}
				else if(dataPtr->eventtype == EQOnlineEventType && thiz_obj->iOnlineNotifier.GetCount())
				{
					for (int i=0;i<thiz_obj->iOnlineNotifier.GetCount();i++)
					{
						MQOnlineNotifier* n = thiz_obj->iOnlineNotifier.GetAt(i);
						if(!n) continue;

						if(dataPtr->totalSize == 0)
							n->NotifyOnlineProgress(filename,dataPtr->received);
						else
							n->NotifyOnlineProgress(filename,dataPtr->received,dataPtr->totalSize,dataPtr->per);
					}
				}
			}
		}
		break;
	case EQGetLinkEventType:
		{
			if(ntf_data)
			{
				get_link_event_data_t* dataPtr = (get_link_event_data_t*)ntf_data;
				CQWString filename;
				filename.AppendData((LPCWSTR)(dataPtr->filename),cx_strlen_w(dataPtr->filename));

				if(dataPtr->eventtype == EQDownloadEventType && thiz_obj->iDownloadNotifier)
				{
					thiz_obj->iDownloadNotifier->HandleGetLinkEvent(filename,dataPtr->chapterid,dataPtr->event,dataPtr->data);			
				}
				else if(dataPtr->eventtype == EQOnlineEventType && thiz_obj->iOnlineNotifier.GetCount())
				{
					for (int i=0;i<thiz_obj->iOnlineNotifier.GetCount();i++)
					{
						MQOnlineNotifier* n = thiz_obj->iOnlineNotifier.GetAt(i);
						if(!n) continue;												
						n->HandleOnlineGetLinkEvent(filename,dataPtr->chapterid,dataPtr->event,dataPtr->data);	
						
					}
				}
					
			}
		}
		break;
	case EQLaunchTaskType:
		{
			if(thiz_obj->iDownloadNotifier)
				thiz_obj->iDownloadNotifier->NotifyLaunchTask();
		}
		break;
	case EQDownLimit:
		{
			if(thiz_obj->iDownloadNotifier)
				thiz_obj->iDownloadNotifier->NotifyDownLimit();
		}
		break;
	default:
		break;
	}
}

void CAddonBookCityStub::__OnUpdateObserverCallback(ADDON_REF(IObject)sender , int ntf_status, void * ntf_data )
	{
	unsigned long ret_val;
	CAddonBookCityStub * thiz_obj;
	CQWString url;
	CQWString version;

	ret_val = IAddonBookCityObserver_GetUserData( sender, (void **)&thiz_obj );

	if ( ADDON_OK != ret_val )
	return;

	update_data_ptr_t update_data = (update_data_ptr_t)ntf_data;
	url.SetData((LPCWSTR)update_data->url);
	if(update_data->intro)
	version.SetData((LPCWSTR)update_data->intro);
	//else
	//version.AppendData(L"")
	thiz_obj->iUpdateNotifier->HandleUpdateEvent(url,ntf_status,version, update_data->seq);
	}

QVOID CAddonBookCityStub::Operate(CQWString& filename)
	{
	if (NULL == iAddonView)
		return;

	//int bookId = bookid.ToINT();
	unsigned long ret_val;

	ret_val = IAddonBookCityView_Operate(iAddonView,(unsigned short*)filename.GetDataPtr());
	}

QVOID CAddonBookCityStub::LaunchTask(QBOOL aIsLaunch)
	{
	if (NULL == iAddonView)
		return;

	//int bookId = bookid.ToINT();
	unsigned long ret_val;

	ret_val = IAddonBookCityView_LaunchTask(iAddonView,aIsLaunch);
	}

QVOID CAddonBookCityStub::Delete(CQWString& filename, QBOOL aIsDeleteFile)
	{
	if (NULL == iAddonView)
		return;

	//int bookId = bookid.ToINT();
	unsigned long ret_val;

	ret_val = IAddonBookCityView_Delete(iAddonView,(unsigned short*)filename.GetDataPtr(),aIsDeleteFile);
	}

QVOID CAddonBookCityStub::DeleteAll()
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_DeleteAll(iAddonView);
	}

QVOID CAddonBookCityStub::StartAll()
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_StartAll(iAddonView);
	}

QVOID CAddonBookCityStub::PauseAll()
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_PauseAll(iAddonView);
	}

QVOID CAddonBookCityStub::Save()
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_Save(iAddonView);
	}

QVOID CAddonBookCityStub::SetIsDownload(bool aIsDown)
	{
	if (NULL == iAddonView)
		return;

	unsigned long ret_val;
	ret_val = IAddonBookCityView_SetIsDownload(iAddonView,aIsDown);
	}

unsigned long CAddonBookCityStub::OpenPreChapter(int bof)
	{
	unsigned long ret_val = 1;

	if (NULL == iAddonView)
		return ret_val;
	ret_val = IAddonBookCityView_OpenPreChapter( iAddonView, bof );
	return ret_val;
	}

unsigned long CAddonBookCityStub::OpenNextChapter(int bof)
	{
	unsigned long ret_val = 1;

	if (NULL == iAddonView)
		return ret_val;

	ret_val = IAddonBookCityView_OpenNextChapter(iAddonView, bof);
	return ret_val;
	}

unsigned long CAddonBookCityStub::OpenLocalNextChapter(int bof)
	{
	unsigned long ret_val = 1;

	if (NULL == iAddonView)
		return ret_val;

	ret_val = IAddonBookCityView_OpenLocalNextChapter(iAddonView, bof);
	return ret_val;
	}

unsigned long CAddonBookCityStub::LoadNextChapter(int bof)
	{
	unsigned long ret_val = 1;

	if (NULL == iAddonView)
		return ret_val;

	ret_val = IAddonBookCityView_LoadNextChapter(iAddonView, bof);
	return ret_val;
	}

unsigned long CAddonBookCityStub::OpenCurrentChapter()
	{
	unsigned long ret_val = 1;

	if (NULL == iAddonView)
		return ret_val;

	ret_val = IAddonBookCityView_OpenCurrentChapter(iAddonView);
	return ret_val;
	}

unsigned long CAddonBookCityStub::OpenCacheFile(CQWString& aFileName)
	{
	unsigned long ret_val = 1;

	if (NULL == iAddonView)
		return ret_val;

	if (aFileName.GetLength() == 0)
		return ret_val;

	ret_val = IAddonBookCityView_OpenCacheFile(iAddonView,(unsigned short*)(aFileName.GetDataPtr()));
	return ret_val;
	}

//QVOID CAddonBookCityStub::SetOnlineRecord(CQLibProvider* aLib)
//{
//	if(NULL == iAddonView)
//		return;
//
//	unsigned long	ret_val;
//
//	ret_val = IAddonBookCityView_OnlineRecord(iAddonView, aLib);
//}

QVOID CAddonBookCityStub::SetNotifier(MQHttpNotifier * aNotifier)
	{
	if (!aNotifier)
		return;
	iNotifier.Add(aNotifier);
	}

QVOID CAddonBookCityStub::SetDownloadNotifier(MQDownloadNotifier * aNotifier)
	{
	iDownloadNotifier = aNotifier;
	}

QVOID CAddonBookCityStub::SetImageNotifier(MQDownloadNotifier * aNotifier)
	{
	iImageNotifier = aNotifier;
	}

QVOID CAddonBookCityStub::SetOnlineNotifier(MQOnlineNotifier * aNotifier)
	{
	if (!aNotifier)
		return;
	iOnlineNotifier.Add(aNotifier);
	}

QVOID CAddonBookCityStub::SetUpdateNotifier(MQUpdateNotifier * aNotifier)
	{
	iUpdateNotifier = aNotifier;
	}

QVOID CAddonBookCityStub::RequestUpgrade()
	{
	if (NULL == iAddonView)
		return;

	IAddonBookCityView_RequestUpgrade(iAddonView);
	}

QVOID CAddonBookCityStub::SetUpdateData(QDateTime& time, int seq)
	{
	Upgrade_time = time;
	update_seq = seq;
	}

QVOID CAddonBookCityStub::GetUpdateData(QDateTime & time, int& seq)
	{
	if (NULL == iAddonView)
		return;
	IAddonBookCityView_GetUpdateData(iAddonView,&time, seq);
	}

QVOID CAddonBookCityStub::ResponseSuggest(CQWString& suggestion,
		CQWString & content)
	{
	if (NULL == iAddonView)
		return;

	IAddonBookCityView_CancleRequest(iAddonView);
	IAddonBookCityView_Suggest_Response(iAddonView,(unsigned short *)suggestion.GetDataPtr(),(unsigned short *)content.GetDataPtr());
	}

QBOOL CAddonBookCityStub::TestDownloadComp()
	{
	if (NULL == iAddonView)
		return ADDON_FAILED;

	unsigned long ret_val;

	ret_val = IAddonBookCityView_TestDownloadComp(iAddonView);

	return ret_val;
	}

void CAddonBookCityStub::__OnStoreObserverCallback(ADDON_REF(IObject)sender , int ntf_status, void * ntf_data )
	{
	unsigned long ret_val;
	CAddonBookCityStub * thiz_obj;

	//#ifndef TESTDOWNLOADCOMPONENT		
		//#else	
		//	if(ntf_data)
		//	{
		//		message_data_t* dataPtr = (message_data_t*)ntf_data;
		//		switch(ntf_status)
		//			{
		//				case Online_Add:
		//				{
		//					dataPtr->message = EMessage_BookLib_Online_Add;					
		//				}
		//				break;
		//				case Download_GetDownloadInfoList:
		//				{
		//					dataPtr->message = EMessage_BookLib_Download_GetDownloadInfoList;										
		//				}
		//				break;
		//				case Download_RemoveOneDownloadInfoList:
		//				{
		//					dataPtr->message = EMessage_BookLib_Download_RemoveOneDownloadInfoList;															
		//				}
		//				break;
		//				case Download_RemoveAllDownloadInfoList:
		//				{
		//					dataPtr->message = EMessage_BookLib_Download_RemoveAllDownloadInfoList;																				
		//				}
		//				break;
		//				case Download_PutDownloadInfoList:
		//				{
		//					dataPtr->message = EMessage_BookLib_Download_PutDownloadInfoList;																									
		//				}
		//				break;
		//		
		//			}		
		//		dataPtr->toModlueid = EModule_Controller_BookLibrary;
		//		cx_sendmessage(dataPtr->message,dataPtr->wParam,dataPtr->lParam,dataPtr->toModlueid,dataPtr->fromModuleid);
		//	}	
		//#endif	
		}
